package com.jifeng.api.uaa.dao;

import com.jifeng.api.crud.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Repository
public class UaaUserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //根据账号查询用户信息
    public UserEntity getUserByUsername(String account){
        String sql = "select * from tb_user where account = ?";
        //连接数据库查询用户
        List<UserEntity> list = jdbcTemplate.query(sql, new Object[]{account}, new BeanPropertyRowMapper<>(UserEntity.class));
        if(list !=null && list.size()==1){
            return list.get(0);
        }
        return null;
    }

    //根据用户id查询用户权限
//    public List<String> findPermissionsByUserId(String userId){
//        String sql = "SELECT * FROM t_permission WHERE id IN(\n" +
//                "\n" +
//                "SELECT permission_id FROM t_role_permission WHERE role_id IN(\n" +
//                "  SELECT role_id FROM t_user_role WHERE user_id = ? \n" +
//                ")\n" +
//                ")\n";
//
//        List<PermissionDto> list = jdbcTemplate.query(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(PermissionDto.class));
//        List<String> permissions = new ArrayList<>();
//        list.forEach(c -> permissions.add(c.getCode()));
//        return permissions;
//    }
}
